package com.chis.cq;

import com.chis.jm.pojo.sys.TsUnit;
import com.chis.jm.pojo.sys.TsZone;
import com.chis.jmdatatimer.comm.TxMgmtModuleMysql;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @Description 
 * @Company zwx
 * @author wlj
 * @version 2019年12月30日
 */
public class UnitImport {
    public static void main(String[] args) throws Exception {
        File file1 = new File("C:\\Users\\Administrator\\Desktop\\国家对接二阶段\\重庆市\\重庆\\数据导入\\chongqing1.txt");
        FileReader m = new FileReader(file1);
        BufferedReader reader = new BufferedReader(m);
        Map<String,String> zonemap = new HashMap<>();
        while (true) {
            String nextline = null;
            try {
                nextline = reader.readLine();
                if (nextline != null) {
                    String[] strings = nextline.split("#");
                    zonemap.put(strings[0]+"00",strings[1]);

                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (nextline == null) break;
        }


        file1 = new File("C:\\Users\\Administrator\\Desktop\\国家对接二阶段\\重庆市\\重庆\\数据导入\\unit5.txt");
        m = new FileReader(file1);
        reader = new BufferedReader(m);
        while (true) {
            String nextline = null;
            try {
                nextline = reader.readLine();
                if (nextline != null) {
                    String[] units = nextline.split("#");
                    String zonegb = units[0].length() == 6 ? units[0]+"0000":units[0]+"00";
                    String unitcode = units[1];
                    String unitcodegj = units[2];
                    String unitname= units[3];
                    String unitnamesim = units[4];
                    String orgcode = units[5].length()>18 ? "暂无":units[5];
                    String addrdetail = units[6];
                    String addrdoor = units[7];
                    String uniter = units[8];
                    String unittel = units[9];
                    String unitaddrcode = units[10]+"00";
                    String regcode = "";
                    if(units.length > 11){
                        regcode = units[11];
                    }
                    StringBuilder sb = new StringBuilder();
                    sb.append("INSERT INTO TS_UNIT(UNIT_CODE,ZONE_GB,UNITNAME,UNIT_SIMPNAME,UNIT_CERTNAME,SPLSHT,ORGCODE,UNITADDR,UNITADDR_CODE,UNITADDR_DETAIL,UNITDOOR");
                    sb.append(",UNIT_REGCODE,UNITER,UNITTEL,UNIT_COUNCODE,IF_REVEAL,DELMARK,CREATE_DATE) values(");
                    sb.append(unitcode).append(",").append(zonegb);
                    sb.append(",'").append(unitname).append("','").append(unitnamesim).append("','").append(unitname);
                    sb.append("','").append(ToFirstChar(unitname)).append("☆").append(ToFirstChar(unitnamesim));
                    sb.append("','").append(orgcode).append("','");
                    String addr = zonemap.get(unitaddrcode.substring(0,2)+"00000000")+zonemap.get(unitaddrcode.substring(0,6)+"0000")+zonemap.get(unitaddrcode.substring(0,8)+"00")+addrdetail+addrdoor;
                    sb.append(addr).append("','").append(unitaddrcode).append("','").append(addrdetail).append("','").append(addrdoor);
                    sb.append("','").append(regcode).append("','").append(uniter).append("','").append(unittel).append("','").append(unitcodegj).append("'");
                    //sb.append(",1,0,NOW()) ON DUPLICATE KEY UPDATE ZONE_GB=VALUES(ZONE_GB),UNITNAME=VALUES(UNITNAME),UNIT_SIMPNAME=VALUES(UNIT_SIMPNAME),UNIT_CERTNAME=VALUES(UNIT_CERTNAME),SPLSHT=VALUES(SPLSHT),ORGCODE=VALUES(ORGCODE),UNITADDR=VALUES(UNITADDR),UNITADDR_CODE=VALUES(UNITADDR_CODE),UNITADDR_DETAIL=VALUES(UNITADDR_DETAIL),UNITDOOR=VALUES(UNITDOOR),UNIT_REGCODE=VALUES(UNIT_REGCODE),UNITER=VALUES(UNITER),UNITTEL=VALUES(UNITTEL),UNIT_COUNCODE=VALUES(UNIT_COUNCODE);");
                    sb.append(",1,0,NOW());");
                    System.out.println(sb.toString());



                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (nextline == null) break;
        }
    }

    public static String ToFirstChar(String chinese) {
        chinese = chinese.replaceAll("（","").replaceAll("）","");
        String pinyinStr = "";
        char[] newChar = chinese.toCharArray();  //转为单个字符
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < newChar.length; i++) {
            if (newChar[i] > 128) {
                try {
                    pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0].charAt(0);
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                pinyinStr += newChar[i];
            }
        }
        return pinyinStr;
    }
}
